#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from zhineng_conn import *
from config_tools import *
from lib.load_data import *
import time
import math
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_from_mongo(date):
    mongo_conn = set_daily_conn()
    print "mongodb connected"
    stat_conn = set_stats_conn()
    dict_conn = set_dict_conn()
    day = date.replace('-',"")
    
    line_list = []
    stat_table='channel_daily_log'
    log_table = stat_table

    query = {'day':day}
    items = find_items(mongo_conn,log_table,query)
    for item in items:
        #print
        try:
            log_id = item['_id']
            channel = item['channel']
            clk = item['clk']
            ctr = item['ctr']
            #day = item['day']
            disp = 0
            try:
                disp = item['disp']
            except Exception:
                pass
            
            edisp = item['edisp']
            entitytype = 0
            try:
                entitytype = item['entitytype']
            except Exception:
                pass
            
            pv = 0
            try:
                pv = item['pv']
            except Exception:
                pass
            
            epv =item['epv']
            income = item['income']
            price = item['price']
            
            #channel = '%s'%channel
            cbm = channel.split('_')
            #print channel
            city = cbm[0]
            cate_id = cbm[1]
            
            city_id = 0
            city_name = ''
            area_id = 0
            area_name = ''
            local_id= 0
            local_name = ''
            if city != '-' or city != '':
                dc = get_citys(city,dict_conn)
                if dc != None:
                    if len(dc) >0:
                        city_id = dc[0][0]
                        city_name = dc[0][1]
                    if len(dc)>1:
                        area_id = dc[1][0]
                        area_name = dc[1][1]
                    if len(dc)>2:
                        local_id= dc[2][0]
                        local_name = dc[2][1]
                    
            cat1 = 0
            cat1name = ''
            cat2 = 0
            cat2name=''
            cat3 = 0
            cat3name=''
            dc = get_cates(cate_id,dict_conn)
            if dc != None:
                if len(dc) > 0:
                    cat1 = dc[0][0]
                    cat1name = dc[0][1]
                if len(dc) > 1:
                    cat2 = dc[1][0]
                    cat2name=dc[1][1]
                if len(dc) > 2:
                    cat3 = dc[2][0]
                    cat3name=dc[2][1]
                    
            param = [log_id,city_id,city_name,area_id,area_name,local_id,local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,clk,ctr,date,disp,edisp,pv,epv,income,price,entitytype]
            line_list.append('\t'.join(map(lambda i : str(i), param)))
        except Exception:
            pass
    cont = '\n'.join(line_list)
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/daily/"
    file_name = "channel_daily_log%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)    
    load_data(stat_conn,path+file_name,stat_table,'\t')
    
    dict_conn.close()
    stat_conn.close()

def execute(date):
    get_from_mongo(date)
    try:
        pass
        #get_from_mongo(date)
    except Exception,e:
        print 'get_from_mongo:',e
    try:
        pass
        #get_top_info_cate_stat(date)
        #copy_from_mongo(date)
    except Exception,e:
        print 'get_top_info_cate_stat:',e

    
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
